﻿using famails.App_Code.source.dto;
using famails.bus;
using famails.dto;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace famails.BackEnd.Customer
{
    public partial class OrderList : System.Web.UI.Page
    {
        static log4net.ILog logs = log4net.LogManager.GetLogger("ErrorRollingLogFileAppender");
        static UserLoginDTO userLogin = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            userLogin = getUserLogin();
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private UserLoginDTO getUserLogin()
        {
            if (Session["us-login"] != null)
            {
                return (UserLoginDTO)Session["us-login"];
            }
            else Response.Redirect("~");
            return null;
        }
        private void LoadData()
        {
            try
            {
                CM_GroupBUS ulBus = new CM_GroupBUS();
                DataTable table = ulBus.GetAll2Order(getUserLogin().UserId);

                ddlGroup.DataTextField = "GroupName";
                ddlGroup.DataValueField = "GroupID";

                DataRow row = table.NewRow();
                row["GroupID"] = -100;
                row["GroupName"] = "Tất cả";
                table.Rows.InsertAt(row, 0);
                ddlGroup.DataSource = table;
                ddlGroup.DataBind();
            }
            catch (Exception ex)
            {

                pnSuccess.Visible = false;
                pnError.Visible = true;
                lblError.Text = " Đã xảy ra lỗi trong quá trình thực hiện. Vui lòng thử lại !<br/>" + ex.Message;
                logs.Error(userLogin.Username + "-Customer-Group - LoadData", ex);
            }
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<CM_OrderDTO> Search(string ordercode, string deliverycode, string name, string phone, string email, string from, string to, int groupid)
        {
            userLogin = (UserLoginDTO)HttpContext.Current.Session["us-login"];
            List<CM_OrderDTO> result = new List<CM_OrderDTO>();
            try
            {
                //string name = "", phone = "h", email = ""; int group = -1;
                CM_OrderBUS ctBUS = new CM_OrderBUS();
                DataTable customerBySelect = ctBUS.Search(ordercode, deliverycode,email, phone, name, from, to, groupid, userLogin.UserId);
                CM_OrderDTO item = new CM_OrderDTO();
                foreach (DataRow dr in customerBySelect.Rows)
                {
                    item = new CM_OrderDTO();
                    item.CustomerID = Convert.ToInt32(dr["CustomerID"] + "");
                    item.OrderID = Convert.ToInt32(dr["OrderID"] + "");
                    item.CustomerName = dr["CustomerName"] + "";
                    item.DeliveryCode = dr["DeliveryCode"] + "";
                    item.OrderCode = dr["OrderCode"] + "";
                    item.Amount = Convert.ToDecimal(dr["Amount"] + "");
                    item.DeliveryFee = Convert.ToDecimal(dr["DeliveryFee"] + "");
                    item.Deposit = Convert.ToDecimal(dr["Deposit"] + "");
                    item.Delivery = dr["Delivery"] + "";
                    item.DeliveryDate = Convert.ToDateTime(dr["DeliveryDate"] + "");
                    item.Payment = dr["Payment"] + "";
                    item.Status = dr["Status"] + "";
                    item.OrderDate = Convert.ToDateTime(dr["OrderDate"] + "");
                    result.Add(item);
                }
            }
            catch (Exception ex)
            {
                logs.Error("Order - Search", ex);
            }
            return result;
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void Delete(int id)
        {
            try
            {
                CM_OrderBUS ctBUS = new CM_OrderBUS();
                ctBUS.Delete(id);
            }
            catch (Exception ex)
            {
                logs.Error("Order - Delete", ex);
            }
        }
    }
}